// src/store/modules/parking.ts
import { reqGetParkingList } from "@/api/index";
import { Module } from "vuex";

// 定义停车数据的接口
export interface ParkingData {
  key: number;
  parkingSpot: string;
  entryTime: string;
  licensePlate: string;
  color: string;
  imgSrc: string;
}

interface ParkingResponse {
  code: number;
  data: ParkingData[];
}

interface ParkingState {
  parkingList: ParkingData[];
}

const state: ParkingState = {
  parkingList: [], // 停车数据
};

const mutations = {
  GETPARKINGLIST(state: ParkingState, parkingList: ParkingData[]) {
    state.parkingList = parkingList;
  },
};

const actions = {
  async getParkingList({
    commit,
  }: {
    commit: (type: string, payload?: ParkingData[]) => void;
  }) {
    const res: ParkingResponse = await reqGetParkingList(); // 调用 API 请求
    if (res.code === 200) {
      commit("GETPARKINGLIST", res.data); // 提交数据到 mutations
    }
  },
};

const getters = {};

const parkingModule: Module<ParkingState, any> = {
  state,
  mutations,
  actions,
  getters,
};

export default parkingModule;
